/*
 * @Description:部门的controller
 * @Version: 2.0
 * @Autor: kafei
 * @Date: 2021-11-26 16:45:56
 * @LastEditors: kafei
 * @LastEditTime: 2021-12-08 14:56:41
 */
const result = require('../utils/result')
const { listToTree, BfsTreeData } = require('../utils/tree')
const { momentResultDateFn } = require('../utils/index.js')
const { PageModel } = require('../model/pageModel')
const {
  getAllDepartMentServer,
  getUserByDeptIdServer
} = require('../server/departMentServer')

// const { findRoleServer } = require('../server/roleServer')

async function getAllDepartMentController() {
  const data = await getAllDepartMentServer()
  return result('0', { data: listToTree(data, { id: 'depId' }) })
}

async function getAllListDepartMentController() {
  const data = await getAllDepartMentServer()
  return result('0', { data })
}

async function getUserByDeptIdController(ctx) {
  const { depId } = ctx.query
  const { page, size } = ctx.query
  if (!depId) {
    return result('1', '部门depId为必传参数!')
  }
  const data = await getAllDepartMentServer()
  const treeData = listToTree(data, { id: 'depId' }) // 列表转成树结构
  const currentNode = BfsTreeData(
    treeData,
    (node) => {
      // 找出当前节点
      if (`${node.depId}` === depId) {
        return node
      }
    },
    { id: 'depId' }
  )
  const resultData = await handleCurrentNode(currentNode, ctx.query) // 处理当前节点业务逻辑函数
  const momentResultDate = momentResultDateFn(resultData) // 转换时间格式
  // const roleResultData = await handleRoleData(momentResultDate) // 处理角色名称回显
  return result('0', { data: createPageModel(page, size, momentResultDate) }) // 创建分页模型
}

// 处理角色名称回显
// async function handleRoleData(data) {
//   if (!data || data.length === 0) {
//     return []
//   }
//   const result = await findRoleServer()
//   console.log(result, 'roleResultData')

//   return data.reduce((cur, next) => {
//     const rid = next.rid
//     const isHave = result.find((v) => v.rid === rid)
//     if (isHave) {
//       const { rName, rdesc, rStatus } = isHave
//       const json = Object.assign({}, next, { rName, rdesc, rStatus })
//       cur.push(json)
//     }
//     return cur
//   }, [])
// }

/**
 * @description: 创建分页的数据结构
 * @param {*}
 * @return {*}
 * @author: kafei
 */
function createPageModel(page, size, data) {
  const result = new PageModel(page, size, data)
  return result
}

/**
 * @description:处理当前节点业务逻辑函数
 * @param {*} node
 * @return {*} 当前节点下面的所有子节点
 * @author: kafei
 */
async function handleCurrentNode(node, params) {
  let resultData = []
  if (node.children && node.children.length > 0) {
    const nodeChilren = getNodeChildren(node, [])
    if (nodeChilren.length > 0) {
      const nodeId = nodeChilren.map((v) => v.depId)
      for (let i = 0; i < nodeId.length; i++) {
        // const result = await getUserByDeptIdServer(nodeId[i])
        const targetParams = Object.assign({}, params, { depId: nodeId[i] })
        const result = await getUserByDeptIdServer(targetParams)
        resultData = resultData.concat(...result)
      }
    }
  } else {
    const targetParams = Object.assign({}, { depId: node.depId }, params)
    const result = await getUserByDeptIdServer(targetParams)
    resultData = resultData.concat(...result)
  }

  return resultData
}

function getNodeChildren(node, result = []) {
  if (!node || node === null) {
    return result
  }
  const children = node.children
  if (!children || children.length === 0) {
    return result
  }
  for (let i = 0; i < children.length; i++) {
    const target = children[i]

    if (!target.children || target.children.length === 0) {
      result.push(target)
    } else {
      getNodeChildren(target, result)
    }
  }
  return result
}

module.exports = {
  getAllDepartMentController,
  getUserByDeptIdController,
  getAllListDepartMentController
}
